#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <Foundation/Foundation.h>

#include "render.h"
#include "system.h"

int ufoRenderText(const char *text, ufoRect *rect, const char *font, int fontSize, int bold, ufoColor color, int format, unsigned char *output)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSString *labelString = [NSString stringWithUTF8String:text];
    
    UIFont *uiFont = 0;
    if (!bold)
        uiFont = [UIFont systemFontOfSize:fontSize*ufoGetDPI()];
    else
        uiFont = [UIFont fontWithName:@"STHeitiSC-Medium" size:fontSize*ufoGetDPI()];//[UIFont boldSystemFontOfSize:fontSize*ufoGetDPI()*(19.0/22.0)];
    
    //NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:fontSize*(19.0/23.0)], NSFontAttributeName, [NSColor colorWithCalibratedRed:color.red green:color.green blue:color.blue alpha:1.0], NSForegroundColorAttributeName, nil];
    
    CGSize textSize = [labelString sizeWithFont:uiFont];
    
    int width = textSize.width + 1;
    int height = textSize.height + 1;
    
    rect->right = rect->left + width;
    rect->bottom = rect->top + height;
    
    if (format & UFO_TEXT_CALCRECT) {
        [pool release];
        return 1;
    }
    
    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(textSize, NO, 0.0);
    else
        // iOS is < 4.0
        UIGraphicsBeginImageContext(textSize);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    memset(output, 0, width*height*4);
    CGContextRef context = CGBitmapContextCreate (output, width, height, 8, width*4, colorSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorSpace);
    
    UIGraphicsPushContext(context);
    
    CGRect offscreenRect = {0.0, 0.0, width, height};
    [[UIColor colorWithRed:color.red green:color.green blue:color.blue alpha:1.0f ] setFill];
    [labelString drawInRect:offscreenRect withFont:uiFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
    
    UIGraphicsPopContext();
    
    CGContextRelease(context);
    
    [pool release];
    return 1;
}

int ufoBeginRender(void* param)
{
    EAGLContext *context = (EAGLContext*)param;
    [EAGLContext setCurrentContext:context];
    return 1;
}

int ufoEndRender(void* param)
{
    if (1) {
        //glBindFramebuffer(GL_FRAMEBUFFER, 1);
        //glBindRenderbuffer(GL_RENDERBUFFER, 1);
        
        EAGLContext *context = (EAGLContext*)param;
        [context presentRenderbuffer:GL_RENDERBUFFER];
    }
    else{
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, 1);
        glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, 2);
        glResolveMultisampleFramebufferAPPLE();
    
        GLenum attachments[] = {GL_COLOR_ATTACHMENT0};
        glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 1, attachments);
    
        glBindRenderbuffer(GL_RENDERBUFFER, 1);

        EAGLContext *context = (EAGLContext*)param;
        [context presentRenderbuffer:GL_RENDERBUFFER];
    
        glBindFramebuffer(GL_FRAMEBUFFER, 2);
    }
    return 1;
}